package com.rain.shoppingscienceproj.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rain.shoppingscienceproj.module.UserInfo;
import com.rain.shoppingscienceproj.utils.LoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Rain on 2017/12/9.
 */

public abstract class BaseFragment extends Fragment {
    private FragmentActivity activity;

    private View parentView;
    // 标志位 标志已经初始化完成。
    protected boolean isPrepared;

    //标志位 fragment是否可见
    protected boolean isVisible;
    public Unbinder bind;
    public static final String TAG = "============";
    public UserInfo userInfo;

    public LoadingDialog loadingDialog;

    public abstract
    @LayoutRes
    int getLayoutResId();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        parentView = inflater.inflate(getLayoutResId(), container, false);
        activity = getSupportActivity();
        bind = ButterKnife.bind(this, parentView);
        loadingDialog = new LoadingDialog(getActivity(), "正在加载...");
        return parentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        finishCreateView(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (FragmentActivity) activity;
    }

    protected void onVisible() {
        lazyLoad();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        this.activity = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        bind.unbind();
    }

    public FragmentActivity getSupportActivity() {
        return super.getActivity();
    }

    public android.app.ActionBar getSupportActionBar() {
        return getSupportActivity().getActionBar();
    }

    public Context getApplicationContext() {

        return this.activity == null
                ? (getActivity() == null ? null :
                getActivity().getApplicationContext())
                : this.activity.getApplicationContext();
    }

    /**
     * Fragment数据的懒加载
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {

        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    public <T extends View> T $(int id) {
        return (T) parentView.findViewById(id);
    }

    public abstract void finishCreateView(Bundle bundle);

    protected void lazyLoad() {
    }

    protected void onInvisible() {
    }

    protected void loadData() {
    }


    protected void showProgressBar() {
    }


    protected void hideProgressBar() {
    }


    protected void initRecyclerView() {
    }


    protected void initRefreshLayout() {
    }

    public void startActiviys(Class c) {
        Intent intent = new Intent(getActivity(), c);
        startActivity(intent);
    }

    public void startActiviys(Class c, int type) {
        Intent intent = new Intent(getActivity(), c);
        intent.putExtra("type", type);
        startActivityForResult(intent, 1);
    }

    public void startActiviys(Class c, String type) {
        Intent intent = new Intent(getActivity(), c);
        intent.putExtra("type", type);
        startActivityForResult(intent, 1);
    }

    public void startActiviys(Class c, String type, String shopId) {
        Intent intent = new Intent(getActivity(), c);
        intent.putExtra("type", type);
        intent.putExtra("shopId", shopId);
        startActivityForResult(intent, 1);
    }
    public void startActivitys(Class c, String type, String shopId) {
        Intent intent = new Intent(getActivity(), c);
        intent.putExtra("type", type);
        intent.putExtra("id", shopId);
        startActivity(intent);
    }
}
